//
//  MeatSendViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/15.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatSendViewController.h"

#import "MeatOrderListCell.h"
#import "MeatOrderLocationCell.h"
#import "MeatPayView.h"
#import "MeatTipLabel.h"

#import "MeatOrderBox.h"


@interface MeatSendViewController ()

/**
 tap
 */
@property (nonatomic, strong) UITapGestureRecognizer *tap;

/**
 当前是第几个section
 */
@property (nonatomic, assign) NSInteger currentIndex;

/**
 提示标签
 */
@property (nonatomic, strong) MeatTipLabel *tipLabel;

/**
 footer
 */
@property (nonatomic, strong) MeatPayView *footer;


@end

@implementation MeatSendViewController

- (void)setupUI{
    
    {
        self.baseNavView.navTitleLabel.text = @"确认赠送";
        self.baseNavView.navTitleLabel.textColor = KWhiteColor;
        
        [self.baseNavView.leftNavBtn setImage:[UIImage imageNamed:@"nav_white"] forState:UIControlStateNormal];
        
        UIImage *image = [UIImage imageNamed:@"meat_question_nav"];
        self.baseNavView.layer.contents =  (__bridge id)image.CGImage;
    }
    
    [self.view addSubview:self.tipLabel];
    
}


- (void)setupLayout{
    
    self.tipLabel.mj_y = TJ_NavBarHeight;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0) {
        return 90;
    }
    return 183;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = mBackgroudColor;
    return sectionView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        MeatOrderLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatOrderLocationCell"];
        cell.type = MeatOrderLocationCellType_Have;
        return cell;
    }
    
    
    MeatOrderListCell *newsCell = [tableView dequeueReusableCellWithIdentifier:@"MeatOrderListCell"];
    newsCell.shopName = @"行走的铺子";
    newsCell.type = indexPath.section == 2 ?MeatOrderListCellType_One :MeatOrderListCellType_More;
    
    kWeakSelf(self);
    newsCell.editBlock = ^(NSInteger params) {
        weakself.currentIndex = indexPath.section;
    };
    
    return newsCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MeatOrderBox *box = [[MeatOrderBox alloc] init];
    [box showView];
    
}


#pragma mark - setter and getter

- (MeatTipLabel *)tipLabel{
    if (!_tipLabel) {
        _tipLabel = [[MeatTipLabel alloc] init];
        _tipLabel.height = 38;
        
        
        
        NSString *contentStr = @"选择被赠送人的收货地址";
        _tipLabel.text = contentStr;
        [_tipLabel sizeToFit];
    }
    return _tipLabel;
}



#pragma mark - override methods

- (void)registerTableViewCell{
    
    [self.tableView registerClass:[MeatOrderListCell class] forCellReuseIdentifier:@"MeatOrderListCell"];
    [self.tableView registerClass:[MeatOrderLocationCell class] forCellReuseIdentifier:@"MeatOrderLocationCell"];
    
    self.tableView.height = KScreenHeight - (TJ_NavBarHeight + 38);
    self.tableView.mj_y = (TJ_NavBarHeight + 38);
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 10)];
    view.backgroundColor = mBackgroudColor;
    self.tableView.tableFooterView = view;
}


#pragma mark - other methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
    CGFloat sectionHeaderHeight = 10;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}


#pragma mark - notification methods

- (void)textFieldTextChanged:(NSNotification *)notification
{
    UITextField *textField = notification.object;
    
    NSLog( @"______ 第 %ld 组  %@",  self.currentIndex, textField.text);
    // self.model.phone = textField.text;
}

- (void)keyboardWillShow:(NSNotification *)notification{
    NSDictionary *info = [notification userInfo];
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    kWeakSelf(self);
    [UIView animateWithDuration:duration animations:^{
        weakself.tableView.mj_y = 0;
    }];
    
    //当键盘升起的时候添加点击降落操作
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    }
    [self.view addGestureRecognizer:self.tap];
}

//取消键盘响应
- (void)hideKeyboard{
    
    [self.view endEditing:YES];
}


- (void)keyboardWillHide:(NSNotification *)notification{
    
    self.tableView.mj_y = 0;
    if (self.tap) {
        [self.view removeGestureRecognizer:self.tap];
    }
}


//添加键盘通知
- (void)addkeyBoardNotification{
    
    [kNotificationCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [kNotificationCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [kNotificationCenter addObserver:self selector:@selector(textFieldTextChanged:) name:UITextFieldTextDidChangeNotification object:nil];
}

//移除键盘通知
- (void)removeKeyboardNotification{
    
    [kNotificationCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [kNotificationCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [kNotificationCenter removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)dealloc{
    
    [self removeKeyboardNotification];
}


@end

